"""
Form Controller
Handles business logic related to forms using Redis caching
"""

from app import RedisManager
from app.models.form import Form
from app.models.user import User
from app import db

class FormController:
    """Controller for form-related operations with Redis caching"""
    
    @staticmethod
    def get_form_with_cache(form_id):
        """
        Retrieve form details with Redis caching
        """
        # Try to get from cache first
        cached_form = RedisManager.get_cached_form(form_id)
        if cached_form:
            return cached_form
        
        # If not in cache, get from database
        form = Form.query.get(form_id)
        if form:
            form_data = {
                'id': form.id,
                'user_id': form.user_id,
                'name': form.name,
                'description': form.description,
                'form_type': form.form_type,
                'structure': form.structure,
                'created_at': form.created_at.isoformat() if form.created_at else None,
                'updated_at': form.updated_at.isoformat() if form.updated_at else None,
                'archived_at': form.archived_at.isoformat() if form.archived_at else None,
                'deleted_at': form.deleted_at.isoformat() if form.deleted_at else None
            }
            
            # Cache the form data
            RedisManager.cache_form(form_id, form_data)
            return form_data
        
        return None
    
    @staticmethod
    def get_user_forms_with_cache(user_id):
        """
        Retrieve user forms list with Redis caching
        """
        # Try to get from cache first
        cached_forms = RedisManager.get_cached_user_forms(user_id)
        if cached_forms:
            return cached_forms
        
        # If not in cache, get from database
        forms = Form.query.filter_by(user_id=user_id).all()
        forms_data = []
        for form in forms:
            form_data = {
                'id': form.id,
                'name': form.name,
                'description': form.description,
                'form_type': form.form_type,
                'created_at': form.created_at.isoformat() if form.created_at else None
            }
            forms_data.append(form_data)
        
        # Cache the forms list
        RedisManager.cache_user_forms(user_id, forms_data)
        return forms_data
    
    @staticmethod
    def get_form_submissions_count(form_id):
        """
        Get form submissions count from Redis
        """
        return RedisManager.get_form_submissions_count(form_id)
    
    @staticmethod
    def increment_form_submissions_count(form_id):
        """
        Increment form submissions count in Redis
        """
        return RedisManager.increment_form_submissions_count(form_id)
    
    @staticmethod
    def create_form(user_id, name, description, form_type, structure):
        """
        Create a new form and invalidate cache
        """
        form = Form(
            user_id=user_id,
            name=name,
            description=description,
            form_type=form_type,
            structure=structure
        )
        
        db.session.add(form)
        db.session.commit()
        
        # Invalidate user forms cache
        RedisManager.delete_user_cache(user_id)
        
        return form